package com.comleader.operator.validate;

import static com.comleader.operator.common.constant.Constant.RESIDENT_IDCARD;

import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.regex.Pattern;

import com.comleader.operator.pojo.entity.DictDataEntity;
import com.comleader.operator.pojo.entity.SysConfigEntity;
import com.comleader.operator.service.DictDataService;
import com.comleader.operator.service.SysConfigService;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;

/**
 * @Dscription 通用校验类
 * @Author 连新景
 * @Date 2024/08/06 15:40
 */
public class CommonValidator {

    public static DictDataService dictDataService = SpringUtil.getBean(DictDataService.class);

    public static SysConfigService sysConfigService = SpringUtil.getBean(SysConfigService.class);

    public static final Pattern addressPattern = Pattern.compile("^(?<province>[^省]+省|[^自治区]+自治区|[^市]+市|[^行政区]+行政区|[^特别行政区]+特别行政区|[^澳门]+澳门|[^香港]+香港)\\s*"
        + "(?<city>[^市]+市|[^区]+区|[^地区]+地区|[^自治州]+自治州|[^盟]+盟|[^县]+县)\\s*" + "(?<county>[^区]+区|[^县]+县|[^市]+市|[^旗]+旗|[^特区]+特区)?\\s*"
        + "(?<town>[^街道]+街道|[^乡]+乡|[^镇]+镇)?\\s*" + "(?<village>[^社区]+社区|[^村]+村)?\\s*" + "(?<address>.+)$");

    // 2-60位，只能是中文和·
    public static final String CHINESE_NAME = "^[\u4E00-\u9FFF·]{2,60}$";

    /**
     * @Description 校验未登记
     * @Auhtor 连新景
     * @Date 2024/08/03 10:05
     * @param fields
     * @return boolean
     */
    public static boolean isUnregister(String... fields) {
        return StrUtil.isAllBlank(fields);
    }

    /**
     * @Description 校验字典值不正确
     * @Auhtor 连新景
     * @Date 2024/08/05 11:35
     * @param dictCode 字典编码
     * @param value 字典值
     * @return boolean
     */
    public static boolean isDictIncorrect(String dictCode, String value) {
        List<DictDataEntity> dictDataList = dictDataService.listDictDataByCode(dictCode);
        return !CollStreamUtil.toList(dictDataList, DictDataEntity::getDictValue).contains(value);
    }

    /*
     * @Description 姓名是否不合规
     * @Auhtor 连新景
     * @Date 2024/08/06 16:34
     * @param entity
     * @param userBusinessType
     * @return boolean
     */
    public static boolean isNameNoncompliant(String ownerName) {
        return StrUtil.isNotBlank(ownerName) && !Validator.isMatchRegex(CHINESE_NAME, ownerName);
    }

    /*
     * @Description 证件类型是否不合规
     * @Auhtor 连新景
     * @Date 2024/08/06 16:34
     * @param entity
     * @param userBusinessType
     * @return boolean
     */
    public static boolean isIdTypeNoncompliant(String dictCode, String ownerIdType) {
        return StrUtil.isNotBlank(ownerIdType) && isDictIncorrect(dictCode, ownerIdType);
    }

    /*
     * @Description 证件号码是否不合规
     * @Auhtor 连新景
     * @Date 2024/08/06 16:34
     * @param entity
     * @param userBusinessType
     * @return boolean
     */
    public static boolean isIdNumberNoncompliant(String ownerIdType, String ownerIdNumber) {
        return RESIDENT_IDCARD.equals(ownerIdType) && StrUtil.isNotBlank(ownerIdNumber) && !IdcardUtil.isValidCard(ownerIdNumber);

    }

    /*
     * @Description 地址是否不合规，只要有一个不合规就认为不合规
     * @Auhtor 连新景
     * @Date 2024/08/06 15:55
     * @param address
     * @return boolean
     */
    public static boolean isAddressNoncompliant(String... address) {
        for (String str : address) {
            if (StrUtil.isNotBlank(str) && !addressPattern.matcher(str).matches()) {
                return true;
            }
        }
        return false;
    }

    public static String selectValueByKey(String key) {
        SysConfigEntity sysConfigEntity = sysConfigService.selectByKey(key);
        if (sysConfigEntity == null) {
            return null;
        }
        return sysConfigEntity.getParamValue();
    }

    public static boolean isDateFormatException(String date, DateTimeFormatter formatter) {
        try {
            DateUtil.parse(date, formatter);
            return false;
        } catch (Exception e) {
            return true;
        }
    }
}
